package com.biwin.module.authority.starter.controller;

import com.biwin.common.app.exception.BwBasicException;
import com.biwin.common.app.exception.BwNotFoundException;
import com.biwin.core.main.controller.BwBasicController;
import com.biwin.module.authority.api.dto.MenuDto;
import com.biwin.module.authority.api.enums.BwMenuExceptionEnum;
import com.biwin.module.authority.api.service.IMenuService;
import com.biwin.module.authority.api.vo.MenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Menu Controller
 *
 * @author biwin
 * @since 0.0.1
 */
@Api(tags = "菜单管理")
@RestController("Menu Controller")
@RequestMapping("/api/auth/menu")
public class MenuController extends BwBasicController {

    @Autowired
    private IMenuService menuService;

    @ApiOperation(value = "菜单列表", httpMethod = "POST")
    @PostMapping("/list")
    public void queryList() {}

    @ApiOperation(value = "添加菜单", httpMethod = "PUT")
    @PutMapping("/add")
    public MenuVo add(@RequestBody @Validated MenuDto menuDto) {
        try {
            menuService.findByName(menuDto.getName());
        } catch (BwNotFoundException e) {
            return menuService.add(menuDto);
        }
        throw new BwBasicException(BwMenuExceptionEnum.MENU_EXIST);
    }

    @ApiOperation(value = "修改菜单", httpMethod = "POST")
    @PostMapping("/update")
    public MenuVo update(@RequestBody @Validated MenuDto menuDto) {
        Assert.notNull(menuDto.getId(), "id must be not null");
        Assert.notNull(menuDto.getVersion(), "version must be not null");
        return menuService.update(menuDto);
    }

    @ApiOperation(value = "删除菜单", httpMethod = "DELETE")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable(name = "id") Long id) {
        return menuService.delete(id);
    }

    @ApiOperation(value = "启用菜单", httpMethod = "POST")
    @PostMapping("/enable/{id}")
    public boolean enable(@PathVariable(name = "id") Long id) {
        return menuService.enable(id);
    }

    @ApiOperation(value = "禁用菜单", httpMethod = "POST")
    @PostMapping("/disable/{id}")
    public boolean disable(@PathVariable(name = "id") Long id) {
       return  menuService.disable(id);
    }
}
